package com.meichis.mcsappframework.photoview;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.meichis.mcsappframework.R;

/**
 * Created by pei on 2016/10/26.
 */

public class ZoomImageActivity extends FragmentActivity {

    private HackyViewPager hvp_container;
    private TextView tv_indicator;
    private String[] imagePaths;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zoomimage);
        hvp_container = (HackyViewPager) findViewById(R.id.hvp_container);
        tv_indicator = (TextView) findViewById(R.id.tv_indicator);

        imagePaths = getIntent().getStringArrayExtra("imagePaths");
        if (imagePaths != null && imagePaths.length > 0) {
            position = getIntent().getIntExtra("position", 0);
            hvp_container.setAdapter(new ImagePagerAdapter(getSupportFragmentManager(), imagePaths));
            hvp_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    tv_indicator.setText((position + 1) + "/" + imagePaths.length);
                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            if (savedInstanceState != null) {
                position = savedInstanceState.getInt("position");
            }
            hvp_container.setCurrentItem(position);
        } else {
            findViewById(R.id.hvp_container).setVisibility(View.GONE);
            PhotoView pv_image = (PhotoView) findViewById(R.id.pv_image);
            pv_image.setVisibility(View.VISIBLE);
            tv_indicator.setText("1/1");
            int resId = getIntent().getIntExtra("resId", 0);
            if (resId != 0)
                pv_image.setImageResource(resId);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("position", hvp_container.getCurrentItem());
    }
}
